// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    // 摄像头
    run: '',
    stop: '',
    total: '',
    //告警信息
    gjtotal: '',
    dealt: '',
    dealted: '',
    // 服务器
    serverTotal: '',
    onLine: '',
    invalid: ''
  },

  jumpHome: function () {
    wx.redirectTo({
      url: 'home',
    })
  },
  jumpWarn: function () {
    wx.redirectTo({
      url: '../warn/warn',
    })
  },
  jumpCamera: function () {
    wx.redirectTo({
      url: '../camera/camera',
    })
  },
  jumpServer: function () {
    wx.redirectTo({
      url: '../server/server',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data
        })

        let header = {
          'context-type': 'application/json',
          'UserToken': that.data.token,
          'Cookie': wx.getStorageSync("sessionid")
        }
        //1. 获取摄像头数据
        wx.request({
          url: 'https://www.2edge.cn/rest/camera/statistics',
          header: header,
          success: function (res) {
            // console.log(res);
            var cookie = res.header["Set-Cookie"];
            if (cookie != null) {
              wx.setStorageSync("sessionid", res.header["Set-Cookie"]);
            }
            that.setData({
              run: res.data.data.run,
              stop: res.data.data.stop,
              total: res.data.data.total,
            })
          }
        })
        //2. 获取告警信息数据
        wx.request({
          url: 'https://www.2edge.cn/rest/alarm/statistics',
          header: header,
          success: function (res) {
            if (!res.data.data.dealt || !res.data.data.dealted) {
              that.setData({
                dealt: '0',
                dealted: '0'
              })
            }
            that.setData({
              gjtotal: res.data.data.total
            })
          }
        })
        //3.获取服务器数据
        wx.request({
          url: 'https://www.2edge.cn/rest/userDevices/statistics',
          header: header,
          success: function (res) {
            console.log();
            
            that.setData({
              serverTotal: res.data.data.total,
              onLine: res.data.data.onLine.length,
              invalid: res.data.data.invalid
            })
          }
        })
        
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})